/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Validation;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
/**
 * @see  aqui se tiene que irse a la base y detectar si existe el assogmtp 
 * @author Admin
 */

/**
 *      <h:form>
                  <h:panelGrid columns="3"> 
		  Enter your email : 
                  <h:inputText id="Assignado" value="#{segi.assignado}" 
			size="20" required="true" label="Email Address">
 			<f:validator validatorId="Validation.inputValidation" />
 		  </h:inputText>
 		  <h:message for="Assignado" style="color:red" />
 		</h:panelGrid>
 		<h:commandButton value="Submit" action="result" />                
                </h:form>
 */
@FacesValidator("Validation.ValidationAssign")
public class ValidationAssign  implements Validator {

	private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\." +
			"[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*" +
			"(\\.[A-Za-z]{2,})$";
 
	private Pattern pattern;
	private Matcher matcher;
 
	public ValidationAssign(){
		  pattern = Pattern.compile(EMAIL_PATTERN);
	}
 
	@Override
	public void validate(FacesContext context, UIComponent component,
			Object value) throws ValidatorException {
 
		matcher = pattern.matcher(value.toString());
		if(!matcher.matches()){
 
			FacesMessage msg = 
				new FacesMessage("E-mail validation failed.", 
						"Invalid E-mail format.");
			msg.setSeverity(FacesMessage.SEVERITY_ERROR);
			throw new ValidatorException(msg);
 
		}
 
	}
}

